package com.yonyougov.devops.cruxcisupport.kubeconfig;

import com.yonyougov.devops.cruxcisupport.util.CruxDevOpsIdGenerator;
import com.yonyougov.devops.fwc.core.dao.BaseDao;
import com.yonyougov.devops.fwc.core.entity.Pagination;
import com.yonyougov.devops.fwc.core.service.impl.BaseServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.Date;

/**
 * t_kube_config服务实现
 */
@Service
public class KubeConfigServiceImpl extends BaseServiceImpl<KubeConfigEntity, BigInteger> implements KubeConfigService {
    private final KubeConfigDao kubeConfigDao;

    public KubeConfigServiceImpl(KubeConfigDao kubeConfigDao) {
        this.kubeConfigDao = kubeConfigDao;
    }

    @Override
    public BaseDao<KubeConfigEntity, BigInteger> getDao() {
        return kubeConfigDao;
    }
    //<!--用户自定义代码开始-->


    /**
     * 根据环境名称查询
     * @param envName 环境名称
     * @return com.yonyougov.devops.cruxcisupport.kubeconfig.KubeConfigEntity
     */
    @Override
    public KubeConfigEntity findByEnvName(String envName) {
        return kubeConfigDao.findBy(KubeConfigEntity.builder().envName(envName).build());
    }

    /**
     * 查询kubeconfig
     * 条件: KubeConfigEntity实体
     *
     * @param build com.yonyougov.devops.cruxcisupport.kubeconfig.KubeConfigEntity
     * @return com.yonyougov.devops.cruxcisupport.kubeconfig.KubeConfigEntity
     */
    @Override
    public Pagination<KubeConfigEntity> findByKubeConfigPageLike(KubeConfigEntity build) {
        Pagination<KubeConfigEntity> pagination = kubeConfigDao.likeBy(build);
        return pagination;
    }

    /**
     * 新增或者更新
     * @param build com.yonyougov.devops.cruxcisupport.kubeconfig.KubeConfigEntity
     */
    @Override
    public void save(KubeConfigEntity build) {
        if (null == build.getId()) {
            build.setId(new CruxDevOpsIdGenerator().generatorId(kubeConfigDao));
            build.setCreateTime(new Date());
            kubeConfigDao.insert(build);
        } else {
            build.setCreateTime(new Date());
            if (StringUtils.isNotEmpty(build.getContent())) {
                kubeConfigDao.update(build);
            }
        }
    }

    //<!--用户自定义代码结束-->
}
